import { Component, AfterViewChecked, EventEmitter } from '@angular/core';
import { TaskService } from '../_common/_service/task.service';
import { Task } from '../_common/_model/task';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.less', '../_common/todo-list.less']
})

export class SearchComponent implements AfterViewChecked {
  resFromTitle: Task[] = [];
  resFromNotes: Task[] = [];
  isPure = true;
  focusEvent = new EventEmitter<boolean>();
  constructor(
    private _taskService: TaskService) { }

  ngAfterViewChecked() {
    this.focusEvent.emit(true);
  }

  search(key): void {
    if (key) {
      this.isPure = false;
      this.resFromTitle = this._taskService.searchFromTitle(key);
      this.resFromNotes = this._taskService.searchFromNotes(key);
    } else {
      this.isPure = true;
      this.resFromTitle.length = 0;
      this.resFromNotes.length = 0;
    }
  }
}
